/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import ejb.EntityManagement;
import entity.Groups;
import entity.Post;
import entity.User;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;

/**
 *
 * @author Raphael
 */


@ManagedBean
@SessionScoped
public class UserBean implements Serializable {
    private static final String INDEX = "/add/index.xhtml";
    private static final String LOGIN = "/add/login.xhtml";
    private static final long serialVersionUID = 1L;
    private static final String STANDARDROUL = "boss";
    private User user;
    private String password;
    private String name;
    private String newName;
    private String newPassword;
    
    @Inject
    private EntityManagement em;

    public UserBean() {
        
    }
    
    
    public String logout(User user) {
        System.out.println(user);
        ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
        ectx.invalidateSession();
        user = null;
        return "/add/index.xhtml?faces-redirect=true";
    }
    
    public boolean isUser(User user) {
        return user.isUser();
    }
    
    public List<Post> getUserPosts(){
        return em.getUserPosts(this.user.getUser_name());
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNewName() {
        return newName;
    }

    public void setNewName(String newName) {
        this.newName = newName;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }
    
    public String regist(){        
        if(!(newName.isEmpty() && newPassword.isEmpty())){
        User newUser= new User(newName,md5(newPassword));
        System.err.println("Hier komme ich an:"+newName+" Pss:"+newPassword);
        User temUser= em.getUser(newName);
        if(temUser==null){
        try{
            Groups u;
        u = em.getGroup(STANDARDROUL);
         if(u!=null){
            newUser.addGroup(u);
            u.addUser(newUser);
            System.err.println("gefunden und jetzt?");
            em.update(newUser);
        }else{
             u=new Groups();
             u.setGroupid(STANDARDROUL);
             newUser.addGroup(u);
            u.addUser(newUser);
            em.update(newUser);
             
         }
        }catch(Exception e){
            
        }
        }else{
             FacesMessage msg = new FacesMessage(
      FacesMessage.SEVERITY_ERROR,
      "Gibs schon", null);
        throw new ValidatorException(msg);
        }
        
        }
        return LOGIN;
    }
    
    private String md5(String s) {
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        BigInteger i = new BigInteger(1,m.digest());
        return String.format("%1$032x", i);         
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}
    
}
